//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package main

import (
	"encoding/json"

	"atomgit.com/openlinksaas-org/api-server/dao/appstore_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/idea_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/sw_dao"
	"github.com/spf13/cobra"
)

var dumpDataPath string

var DumpCmd = &cobra.Command{
	Use: "dump",
}

var dumpMinAppCmd = &cobra.Command{
	Use: "minApp",
	RunE: func(cmd *cobra.Command, args []string) error {
		err := InitConfig()
		if err != nil {
			return err
		}
		cateItemList, err := appstore_dao.CateDao.ListAll(cmd.Context())
		if err != nil {
			return err
		}
		cateListData, err := json.MarshalIndent(cateItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "minApp"}, "cateList.json", cateListData)
		if err != nil {
			return err
		}
		appItemList, err := appstore_dao.AppDao.ListAll(cmd.Context())
		if err != nil {
			return err
		}
		appListData, err := json.MarshalIndent(appItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "minApp"}, "appList.json", appListData)
		if err != nil {
			return err
		}
		for _, appItem := range appItemList {
			//下载图片
			if appItem.BaseInfo.IconFileId != "" {
				data, err := getFileData(cmd.Context(), "globalAppStore", appItem.BaseInfo.IconFileId)
				if err != nil {
					return err
				}
				err = saveFile([]string{dumpDataPath, "minApp", "icons"}, appItem.BaseInfo.IconFileId+".png", data)
				if err != nil {
					return err
				}
			}
			//下载应用
			if appItem.FileId != "" {
				data, err := getFileData(cmd.Context(), "globalAppStore", appItem.FileId)
				if err != nil {
					return err
				}
				err = saveFile([]string{dumpDataPath, "minApp", "apps"}, appItem.FileId+".zip", data)
				if err != nil {
					return err
				}
			}
		}
		return nil
	},
}

var dumpSoftWareCmd = &cobra.Command{
	Use: "softWare",
	RunE: func(cmd *cobra.Command, args []string) error {
		err := InitConfig()
		if err != nil {
			return err
		}
		cateItemList, err := sw_dao.CateDao.List(cmd.Context())
		if err != nil {
			return err
		}
		cateListData, err := json.MarshalIndent(cateItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "softWare"}, "cateList.json", cateListData)
		if err != nil {
			return err
		}
		swItemList, err := sw_dao.SoftwareDao.ListAll(cmd.Context())
		if err != nil {
			return err
		}
		swListData, err := json.MarshalIndent(swItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "softWare"}, "softWareList.json", swListData)
		if err != nil {
			return err
		}
		for _, swItem := range swItemList {
			//下载图片
			if swItem.IconFileId != "" {
				data, err := getFileData(cmd.Context(), "globalSwStore", swItem.IconFileId)
				if err != nil {
					return err
				}
				err = saveFile([]string{dumpDataPath, "softWare", "icons"}, swItem.IconFileId+".png", data)
				if err != nil {
					return err
				}
			}
		}
		return nil
	},
}

var dumpIdeaStoreCmd = &cobra.Command{
	Use: "ideaStore",
	RunE: func(cmd *cobra.Command, args []string) error {
		err := InitConfig()
		if err != nil {
			return err
		}
		cateItemList, err := idea_dao.IdeaStoreCateDao.List(cmd.Context())
		if err != nil {
			return err
		}
		cateListData, err := json.MarshalIndent(cateItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "ideaStore"}, "cateList.json", cateListData)
		if err != nil {
			return err
		}
		storeItemList, err := idea_dao.IdeaStoreDao.List(cmd.Context(), false, "")
		if err != nil {
			return err
		}
		storeListData, err := json.MarshalIndent(storeItemList, "", "  ")
		if err != nil {
			return err
		}
		err = saveFile([]string{dumpDataPath, "ideaStore"}, "storeList.json", storeListData)
		if err != nil {
			return err
		}
		for _, storeItem := range storeItemList {
			ideaItemList, err := idea_dao.StoreIdeaDao.ListByStore(cmd.Context(), storeItem.IdeaStoreId)
			if err != nil {
				return err
			}
			ideaListData, err := json.MarshalIndent(ideaItemList, "", "  ")
			if err != nil {
				return err
			}
			err = saveFile([]string{dumpDataPath, "ideaStore", "ideas"}, storeItem.IdeaStoreId+".json", ideaListData)
			if err != nil {
				return err
			}
		}
		return nil
	},
}

func init() {
	DumpCmd.PersistentFlags().StringVar(&dumpDataPath, "dataPath", "", "dump target path")
	DumpCmd.MarkPersistentFlagRequired("dataPath")
	DumpCmd.AddCommand(dumpMinAppCmd, dumpSoftWareCmd, dumpIdeaStoreCmd)
}
